<?php
namespace app\index\controller;
use think\Controller;
use app\extra\Sql;
use think\Cookie;
class Login extends Controller
{
    public function login(){
        return view('login');
    }
    public function doLogin(){
        $post = request()->post();
        //验证账户是否存在
        $admin_info = Sql::_find('user',['username'=>$post['username']],'id,username,password,status');
        if(!$admin_info){
            $data = ['errorMsg'=>'该账户不存在'];
            return view('lo_err',$data);
        }
        //验证密码是否正确
        if($admin_info['status'] == Sql::STATUS_OFF){
            $data = ['errorMsg'=>'账户已被锁定'];
            return view('lo_err',$data);
        }
        //密码是否正确
        $check_password = password_verify($post['password'],$admin_info['password']);
        if(!$check_password){
            $data = ['errorMsg'=>'密码输入不正确'];
            return view('lo_err',$data);
        }
        if(!empty($post['remember_me'])){
            $expire = 3600*24*7;
        }else{
            $expire = 60*30;
        }
        session_start();
        $_SESSION['user'] = $admin_info;
        Cookie::set('user_id',$admin_info['id'],['prefix'=>'user_','expire'=>$expire]);
        $this->redirect('/index/index');
    }
    public function logout(){
        session_start();
        unset($_SESSION['user']);
        $this->redirect('/index/index');
    }
    public function gongzhong(){
        return view('register-gongzhong');
    }
    public function lvshi(){
        return view('register-lvshi');
    }
    public function check_user(){
        $post = request()->post();
        $username = $post['param'];
        $user_info = Sql::_find('user',['username'=>$username],'id,username');
        if(!$user_info){
            $data['status'] = 'y';
            echo json_encode($data);
        }else{
            echo '用户名已存在';
        }

    }
    public function gongzhong_save(){
        $post = request()->post();
        $data['username'] = $post['username'];
        $data['password'] = password_hash($post['password'],true);
        $data['email'] = $post['email'];
        $data['mobile'] = $post['mobile'];
        $data['type'] = 1;
        $data['time'] = time();
        if(Sql::_save('user',$data)){
            return view('suc');
        }else{
            return view('err');
        }
    }
    public function lvshi_save(){
        $post = request()->post();
        $data['username'] = $post['username'];
        $data['password'] = password_hash($post['password'],true);
        $data['email'] = $post['email'];
        $data['mobile'] = $post['mobile'];
        $data['type'] = 2;
        $data['time'] = time();
        $user_id = Sql::_save('user',$data);
        if($user_id > 0){
            $data1['user_id'] = $user_id;
            $data1['name'] = $post['name'];
            $data1['workfirmname'] = $post['workfirmname'];
            $data1['code'] = $post['code'];
            $data1['qq'] = $post['qq'];
            $data1['adds'] = $post['adds'];
            $data1['work'] = $post['work'];
            if(Sql::_save('lvshi',$data1)){
                return view('suc');
            }else{
                return view('err');
            }
        }
        return view('err');
    }
}